package net.framework.smc.demo.service;

import net.framework.smc.demo.dao.TripJpa;
import net.framework.smc.demo.dao.UserJpa;
import net.framework.smc.demo.po.ResultPo;
import net.framework.smc.demo.po.Trip;
import net.framework.smc.demo.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @authore zyx
 * @date 2018/6/28 15:31
 */
@Service
public class TripService {

    @Autowired
    TripJpa tripJpa;
    @Autowired
    UserJpa userJpa;
    @Autowired
    private SendService sendService;

    public ResultPo listTrip(String type, String userId) throws Exception {
        List<Map<String, String>> list = tripJpa.findByStatus(userId, type);
        return ResultPo.success(list);
    }

    public ResultPo delTrip(String id) throws Exception {
        Trip trip = tripJpa.findById(id).get();
        trip.setDelFlag(1);
        tripJpa.save(trip);
        return ResultPo.success();
    }

    public ResultPo addTrip(Trip trip) throws Exception {
        tripJpa.save(trip);
        return ResultPo.success();
    }

    public ResultPo finishedTrip(String id) throws Exception {
        Trip trip = tripJpa.findById(id).get();
        trip.setStatus(1);
        tripJpa.save(trip);
        User user = userJpa.findById(trip.getOwner()).get();
        sendService.send(user.getUserName() + "");
        return ResultPo.success();
    }

}
